package com.educate.common.web;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * Created by yincs on 2017/3/31.
 */
public class RandomUtils {

    private static final String RANDOM_STR = "abcdefghijklmnopqrstuvwxyz0123456789";
    private static final String RANDOM_STR_CASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static final String RANDOM_NUM = "0123456789";

    public static Random getRandom() {
        return ThreadLocalRandom.current();
    }

    public static String getRandomStr(int length) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < length; ++i) {
            sb.append(RANDOM_STR.charAt(ThreadLocalRandom.current().nextInt(RANDOM_STR.length())));
        }

        return sb.toString();
    }

    public static String getRandomCaseStr(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; ++i) {
            sb.append(RANDOM_STR_CASE.charAt(ThreadLocalRandom.current().nextInt(RANDOM_STR_CASE.length())));
        }
        return sb.toString();
    }


    public static String getRandomNum(int length) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < length; ++i) {
            sb.append(RANDOM_NUM.charAt(ThreadLocalRandom.current().nextInt(RANDOM_NUM.length())));
        }

        return sb.toString();
    }
}
